<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2022-08-26 14:59:55
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Http\Services\System;


use App\Http\Services\Service;
use App\Repositories\AttachmentRepository;
use App\Repositories\IssueRepository;
use DB;

/**
 * Summary of IssueService
 * @property AttachmentRepository $att
 * @property IssueRepository $repository
 * @author sunkaiyuan
 */
class IssueService extends Service
    {

    public function __construct(IssueRepository $repository, AttachmentRepository $att)
        {
        $this->repository = $repository;
        $this->att        = $att;
        }

    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    public function emergstatus($emergency_id)
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v) use ($emergency_id)
            {
            if ($v->emergency_id != $emergency_id) {
                $v->emergency_id = $emergency_id;
                $v->save();
                }
            });
        }
    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0) {
                fail("需求进度【{$v->status->name}】，删除失败！");
                }
            $v->delete();
            $this->att->whereSource_model($v::class)->whereSource_value($v->id)->autodelete();
            });
        }

    protected function save()
        {
        $request = request();
        $gdata   = $this->repository->autosave($request->toArray());

        $uuid = $request->uuid ?? [0];
        $this->att->whereIn("uuid", $uuid)->get()->filter(function ($v) use ($gdata)
            {
            $v->source_model = $gdata::class;
            $v->source_key   = "id";
            $v->source_value = $gdata->id;
            $v->save();
            });
        return $gdata;
        }
    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 1) {
                fail("需求进度【{$v->status->name}】，完成失败！");
                }
            $v->auditor_id = auth()->user()?->employee_id;
            $v->audited_at = date("Y-m-d H:i:s");
            $v->status_id  = 2;
            $v->save();
            });
        }

    protected function stop()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            $v->status_id = 5;
            $v->save();
            });
        }
    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 2) {
                fail("需求进度【{$v->status->name}】，取消失败！");
                }
            $v->auditor_id = null;
            $v->audited_at = null;
            $v->status_id  = 1;
            $v->save();
            });
        }
    protected function track()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0 && $v->status_id != 5 && $v->status_id != 3) {
                fail("需求进度【{$v->status->name}】，跟进失败！");
                }
            $v->tracktor_id = auth()->user()?->employee_id;
            $v->trackted_at = date("Y-m-d H:i:s");
            $v->status_id   = 1;
            $v->save();
            });
        }

    protected function refuse()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            $v->tracktor_id = auth()->user()?->employee_id;
            $v->trackted_at = date("Y-m-d H:i:s");
            $v->status_id   = 3;
            $v->save();
            });
        }
    protected function notrack()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 1) {
                fail("需求进度【{$v->status->name}】，取消跟进失败！");
                }
            $v->tracktor_id = 0;
            $v->trackted_at = null;
            $v->status_id   = 0;
            $v->save();
            });
        }


    }
